package jb.astronomy.project;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.media.audiofx.BassBoost.Settings;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Switch;
import android.widget.Toast;




public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

	
	
	
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.layout.preferences);
            PreferenceManager.setDefaultValues(Preferences.this, R.layout.preferences, false);

            for(int i=0;i<getPreferenceScreen().getPreferenceCount();i++){
             initSummary(getPreferenceScreen().getPreference(i));
             
             
            }
        }

        @Override 
        protected void onResume(){
            super.onResume();
            // Set up a listener whenever a key changes             
            getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
            
    		
        }

        @Override 
        protected void onPause() { 
            super.onPause();
            // Unregister the listener whenever a key changes             
            getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
           
        } 

        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 
            updatePrefSummary(findPreference(key));
           
           
           
            
           
        	
            
            /*---------------------------------------------------------------------------------------------------*/
            
            
            
          //Toast.makeText(getBaseContext(), "Please wait while reloading  the application for changes to take effect", Toast.LENGTH_SHORT).show();
            //String whatsNewTitle = getResources().getString(R.string.Restart);
            //String whatsNewText = getResources().getString(R.string.RestartText);
            //new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(whatsNewTitle).setMessage(whatsNewText).setPositiveButton(
            //R.string.ok, new DialogInterface.OnClickListener() {
            //public void onClick(DialogInterface dialog, int which) {
            //dialog.dismiss();
         
            //restartFirstActivity();
            restartFirstActivity();
           // }
            
           // }).show();
            
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
            
            if (sharedPrefs.getBoolean("WIFI", true)) {
	            
            	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                Editor editor = prefs.edit();
                editor.putBoolean("WIFI", false);
                editor.commit();
            	
            	Intent gpsOptionsIntent = new Intent(  android.provider.Settings.ACTION_WIFI_SETTINGS);  
                startActivityForResult(gpsOptionsIntent,0);
                PreferenceManager.getDefaultSharedPreferences(this);
                
                
                
                
            }
            
            
                 
            if (sharedPrefs.getBoolean("BT", true)) {
              	 
            	SharedPreferences prefsb = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                Editor editorb = prefsb.edit();
                editorb.putBoolean("BT", false);
                editorb.commit();
                
            	Intent gpsOptionsIntent = new Intent(  android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);  
                startActivityForResult(gpsOptionsIntent,0);
                
                
                
            }
            
        }
        

        private void initSummary(Preference p){
           if (p instanceof PreferenceCategory){
                PreferenceCategory pCat = (PreferenceCategory)p;
                for(int i=0;i<pCat.getPreferenceCount();i++){
                    initSummary(pCat.getPreference(i));
                }
            }else{
                updatePrefSummary(p);
            }

        }

        private void updatePrefSummary(Preference p){
            if (p instanceof ListPreference) {
                ListPreference listPref = (ListPreference) p; 
                p.setSummary(listPref.getEntry()); 
            }
            if (p instanceof EditTextPreference) {
                EditTextPreference editTextPref = (EditTextPreference) p; 
                p.setSummary(editTextPref.getText());
                         
            }
  
        }

        private void restartFirstActivity()
        	 {
        	 
        	Intent intent = new Intent(getApplicationContext(), DSCArduinoActivity.class);
        	intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
        	startActivity(intent);
        	
        	       	
        	 } 
        
} 



